﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NETShop.ORM.SqlSugar
{
    public class RedisCache : ICacheService
    {
        readonly static string _conn = NETShop.Core.ConfigConstants.ConfigManager.Config.Redis.Host;
        /// <summary>
        /// 缓存过期时间
        /// </summary>
        readonly static int _expireMinutes = XR.Core.ConfigConstants.ConfigManager.Config.Caching.ExpireMinutes;
        /// <summary>
        /// DB
        /// </summary>
        readonly static StackExchange.Redis.IDatabase cache = StackExchange.Redis.ConnectionMultiplexer.Connect(_conn).GetDatabase();//redis0:6380,redis1:6380,allowAdmin=true



        public void Add<V>(string key, V value)
        {

            cache.Set(key, value);
        }

        public void Add<V>(string key, V value, int cacheDurationInSeconds)
        {
            cache.Set(key, value, cacheDurationInSeconds);
        }

        public bool ContainsKey<V>(string key)
        {
            return cache.KeyExists(key);
        }

        public V Get<V>(string key)
        {
            return cache.Get<V>(key);
        }

        public IEnumerable<string> GetAllKey<V>()
        {
            //var cache1 = cache.Get("SqlSugarDataCache.*");
            return cache.Get("SqlSugarDataCache.*") as IEnumerable<string>;
            //cache.StringGet();
#pragma warning disable CS0162 // 检测到无法访问的代码
            return null;
#pragma warning restore CS0162 // 检测到无法访问的代码
            //return cache.GetAllKeys();
        }

        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
        {
            if (this.ContainsKey<V>(cacheKey))
            {
                return this.Get<V>(cacheKey);
            }
            else
            {
                var result = create();
                this.Add(cacheKey, result, cacheDurationInSeconds);
                return result;
            }
        }

        public void Remove<V>(string key)
        {
            cache.Remove(key);
        }
    }
    //public class ServiceStackRedis
    //{
    //    private readonly int _expirySeconds = -1;
    //    private readonly PooledRedisClientManager _redisClientManager;
    //    private readonly SerializeService _serializeService = new SerializeService();
    //    public ServiceStackRedis(string host, int port, string password, int expirySeconds, long db)
    //    {
    //        _expirySeconds = expirySeconds;
    //        var hosts = new[] { string.Format("{0}@{1}:{2}", password, host, port) };
    //        _redisClientManager = new PooledRedisClientManager(hosts, hosts, null, db, 500, _expirySeconds);
    //    }

    //    public ServiceStackRedis(string host)
    //        : this(host, 6379, null, -1, 0)
    //    {
    //    }

    //    public ServiceStackRedis()
    //        : this("localhost", 6379, null, -1, 0)
    //    {
    //    }

    //    public bool Set(string key, object value)
    //    {
    //        if (key == null) throw new ArgumentNullException("key");

    //        if (_expirySeconds != -1) return Set(key, value, _expirySeconds);

    //        var json = _serializeService.SerializeObject(value);
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            return client.Set(key, json);
    //        }
    //    }

    //    public bool Set(string key, object value, int duration)
    //    {
    //        if (key == null) throw new ArgumentNullException("key");

    //        var json = _serializeService.SerializeObject(value);
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            return client.Set(key, json, DateTime.Now.AddSeconds(duration));
    //        }
    //    }

    //    public T Get<T>(string key)
    //    {
    //        if (key == null) throw new ArgumentNullException("key");

    //        string data;
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            data = client.Get<string>(key);
    //        }
    //        return data == null ? default(T) : _serializeService.DeserializeObject<T>(data);
    //    }
    //    public bool Remove(string key)
    //    {
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            return client.Remove(key);
    //        }
    //    }

    //    public bool RemoveAll()
    //    {
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            try
    //            {
    //                client.FlushDb();
    //                return true;
    //            }
    //            catch (Exception)
    //            {
    //                return false;
    //            }
    //        }
    //    }

    //    public bool ContainsKey(string key)
    //    {
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            return client.ContainsKey(key);
    //        }
    //    }

    //    public List<string> GetAllKeys()
    //    {
    //        using (var client = _redisClientManager.GetClient())
    //        {
    //            return client.SearchKeys("SqlSugarDataCache.*");
    //        }
    //    }
    //}
}
